package com.tk.exam.utils;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;

import java.security.GeneralSecurityException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Slf4j
public class CommonConvert {

    private static TextCipher cipher;
    //手机号码校验
    public static final String MOBILE_PATTERN = "\\d{11}";
    //身份证号码校验
    public static final String IDNUMBER_PATTERN = "\\d{18}|\\d{17}X|\\d{17}x";
    public static final String CHINESE_CHECK= "[\u4e00-\u9fa5]";

    static{
        String salt= SecurityUtils.readSaltFile();
        cipher = new DESTextCipher();
        cipher.init(salt);
    }

    /**
     * 判断字符串中是否含有中文
     * @param
     * @return
     */
    public static boolean isContainChinese(String str) {

        Pattern p = Pattern.compile(CHINESE_CHECK);
        Matcher m = p.matcher(str);
        if (m.find()) {
            return true;
        }
        return false;
    }

    /**
     * 对手机号码进行加密
     *
     * @param target
     * @return
     */
    public static String encryptMobile(String target) {
        if (isHasEncryptionMobile(target)) {
            return target;
        }
        return encrypt(target);
    }
    /**
     * 验证中文
     * @param chinese 中文字符
     * @return 验证成功返回true，验证失败返回false
     */
    public static boolean checkChinese(String chinese) {
        String regex = "^[\u4E00-\u9FA5]+$";
        return Pattern.matches(regex,chinese);
    }

    /**
     * 对身份证进行加密
     *
     * @param target
     * @return
     */
    public static String encryptIdNumber(String target) {
        if (isHasEncryptionIdNumber(target)) {
            return target;
        }
        return encrypt(target);
    }

    /**
     * 对身份证进行解密
     *
     * @param target
     * @return
     */
    public static String decryptIdNumber(String target) {
        if (!isHasEncryptionIdNumber(target)) {
            return target;
        }
        return decrypt(target);
    }

    /**
     * 对手机号码进行解密
     *
     * @param target
     * @return
     */
    public static String decryptMobile(String target) {
        if (!isHasEncryptionMobile(target)) {
            return target;
        }
        return decrypt(target);
    }

    /**
     * 对字段进行加密
     *
     * @param target
     * @return
     */
    private static String encrypt(String target) {
        try {
            return cipher.encrypt(target);
        } catch (GeneralSecurityException ex) {
            log.error("Can't encrypt objectValue.[objectValue={}]", target, ex);
            return target;
        }
    }

    /**
     * 对字段进行解密
     *
     * @param target
     * @return
     */
    private static String decrypt(String target) {
        try {
            return cipher.decrypt(target);
        } catch (Exception ex) {
            log.error("Can't encrypt objectValue.[objectValue={}]", target, ex);
           // System.out.println(ex.getMessage());
            return target;
        }
    }


    /**
     * 手机号码是否已加密
     *
     * @param mobile
     * @return
     */
    public static boolean isHasEncryptionMobile(String mobile) {
        return isHasEncryption(mobile, MOBILE_PATTERN);
    }

    /**
     * 身份证是否已加密
     *
     * @param idnumber
     * @return
     */
    public static boolean isHasEncryptionIdNumber(String idnumber) {
        return isHasEncryption(idnumber, IDNUMBER_PATTERN);
    }

    private static boolean isHasEncryption(String target, String pattern) {
        if (StringUtils.isBlank(target)) {
            return true;
        }
        return !target.matches(pattern);
    }

    /**
     *  格式如下 ************8221
     * @param bankNum
     * @return
     */
    public static String encBankNum(String bankNum){
        String str = "";
        if(StringUtils.isEmpty(bankNum)){
            return null;
        }
        if(bankNum.length()<4){
            return "Must be greater than 4 bits";
        }
        String js = bankNum.substring(0, bankNum.length()-4);
        for (int i=0;i<js.length();i++){
            str =str+"*";
        }
        String s = bankNum.replaceAll("(\\d{"+js.length()+"})(\\d{4})",   str+"$2");
        return s;
    }

    public static void main(String[] args) throws Exception{
        String idNumber = "372502198311102779";
        String salt = "MkMwMzExNTItNjFvMnctbTFzNXMgUOROuEeKZuVkjXeIUzFpMU86cTEz";
        CommonConvert commonConvert = new CommonConvert();

        String mobile = "18910270829";
        /*System.out.println("salt:" + salt);
        System.out.println(commonConvert.checkChinese("我们11"));

        System.out.println(commonConvert.isHasEncryptionMobile(mobile));
        System.out.println(commonConvert.decryptMobile("zkUxgaSz8Piae8xNuE7rVA=="));
        System.out.println(commonConvert.decryptIdNumber("6ILwf+6S4h8FU5IIb1qNEwwLgOAlh6uZ"));*/
        String s = CommonConvert.encBankNum("1234");
        //System.out.println(s);
    }
}
